<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Form_ValidateUser{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $edit = false, $data = null){
        
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validateName = new Zend_Validate();
        $validateUsername = new Zend_Validate();
        $validatePassword = new Zend_Validate();
        $validateRePassword = new Zend_Validate();
        $validateEmail = new Zend_Validate();
        $validateUserGroup = new Zend_Validate();
        
        $optionUsername = array('table' => 'users', 'field' => 'username');
        
        $optionEmail = array('table' => 'users', 'field' => 'email');
        
        if($edit){
            $optionUsername['exclude'] = array ('field' => 'id', 'value' => $params['id']);
            $optionEmail['exclude'] = array ('field' => 'id', 'value' => $params['id']);
        }
        
        $validateName->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 30)), true)
                        ->addValidator(new Zend_Validate_Alpha(true), true);
                        
        $validateUsername->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('min' => 6, 'max' => 30)), true)
                        ->addValidator(new Zend_Validate_Regex('#^[a-zA-Z0-9]+$#'), true)
                        ->addValidator(new Zend_Validate_Db_NoRecordExists($optionUsername), true);
        
        $validatePassword->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('min' => 6, 'max' => 30)));
        
        $validateRePassword->addValidator(new Zend_Validate_NotEmpty, true)
                            ->addValidator(new Zend_Validate_Identical($params['password']), true);
                            
        $validateEmail->addValidator(new Zend_Validate_NotEmpty(), true)
                    ->addValidator(new Zend_Validate_EmailAddress(), true)
                    ->addValidator(new Zend_Validate_Db_NoRecordExists($optionEmail), true);
                    
        $validateUserGroup->addValidator(new Zend_Validate_GreaterThan(0), true);
        
        /*
        * Check Name
        */
        
        if(!$validateName->isValid($params['name'])){
            $this->_messageError['name'] = '<label class="control-label" for="inputError">' . $translate->_('name') . current($validateName->getMessages()) . '</label>';
            $this->_arrData['name'] = '';
            $this->_classError['name'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['name'] = $data['name'];
            }
        }else{
            $this->_messageError['name'] = '';
            $this->_arrData['name'] = $params['name'];
            $this->_classError['name'] = '';
        }
        
        /*
        * Check Username
        */
        
        if(!$validateUsername->isValid($params['username'])){
            $this->_messageError['username'] = '<label class="control-label" for="inputError">' . $translate->_('username') . current($validateUsername->getMessages()) . '</label>';
            $this->_arrData['username'] = '';
            $this->_classError['username'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['username'] = $data['username'];
            }
        }else{
            $this->_messageError['username'] = '';
            $this->_arrData['username'] = $params['username'];
            $this->_classError['username'] = '';
        }
        
        if(!$edit || $edit && strlen($params['password']) > 0){
            /*
            * Check Password
            */
            
            if(!$validatePassword->isValid($params['password'])){
                $this->_messageError['password'] = '<label class="control-label" for="inputError">' . $translate->_('password') . current($validatePassword->getMessages()) . '</label>';
                $this->_arrData['password'] = '';
                $this->_classError['password'] = 'has-error';
                $this->_isError = true;
                
                if($edit){
                    $this->_arrData['password'] = $data['password'];
                }
            }else{
                $this->_messageError['password'] = '';
                $this->_arrData['password'] = $params['password'];
                $this->_classError['password'] = '';
            }
            
            /*
            * Check Repassword
            */
            
            if(!$validateRePassword->isValid($params['repassword'])){
                $this->_messageError['repassword'] = '<label class="control-label" for="inputError">' . $translate->_('repassword') . current($validateRePassword->getMessages()) . '</label>';
                $this->_arrData['repassword'] = '';
                $this->_classError['repassword'] = 'has-error';
                $this->_isError = true;
                
                if($edit){
                    $this->_arrData['repassword'] = $data['repassword'];
                }
            }else{
                $this->_messageError['repassword'] = '';
                $this->_arrData['repassword'] = $params['repassword'];
                $this->_classError['repassword'] = '';
            }
        }else{
            unset($this->_arrData['password']);
            unset($this->_arrData['repassword']);
        }
        
        /*
        * Check Email
        */
        
        if(!$validateEmail->isValid($params['email'])){
            $this->_messageError['email'] = '<label class="control-label" for="inputError">' . $translate->_('email') . current($validateEmail->getMessages()) . '</label>';
            $this->_arrData['email'] = '';
            $this->_classError['email'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['email'] = $data['email'];
            }
        }else{
            $this->_messageError['email'] = '';
            $this->_arrData['email'] = $params['email'];
            $this->_classError['email'] = '';
        }
        
        /*
        * Check Group
        */
        
        if(!$validateUserGroup->isValid($params['user_group'])){
            $this->_messageError['user_group'] = '<label class="control-label" for="inputError">' . $translate->_('user group error') . '</label>';
            $this->_arrData['user_group'] = '';
            $this->_classError['user_group'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['user_group'] = $data['user_group'];
            }
        }else{
            $this->_messageError['user_group'] = '';
            $this->_arrData['user_group'] = $params['user_group'];
            $this->_classError['user_group'] = '';
        }
        
        $status = true;
        
        if(!isset($params['status'])){
            $status = false;
        }
        
        $this->_arrData['status'] = $status;
        
        if($edit){
            $this->_arrData['id'] = $data['id'];
        }
        
        $date = new Zend_Date();
        $curDate = $date->get(Zend_Registry::get('date_format_insert'));
        
        //$info = new Zenddn_System_Info();
        //$user_id = $info->getUserInfo('id');
        
        if(!$edit){
            $this->_arrData['created'] = $curDate;
            //$this->_arrData['created_by'] = $user_id;
        }
        
        $this->_arrData['modified'] = $curDate;
        //$this->_arrData['modified_by'] = $user_id;
        
        if(!$this->isError()){
            $this->_arrData['password'] = sha1($params['password']);
            unset($this->_arrData['repassword']);
        }
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}